package com.qtu.user_service.controller;

import com.qtu.entity.BmMember;
import com.qtu.user_service.service.SellerService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Controller
@RequestMapping("api/seller")
public class SellerController {

    @Autowired
    private SellerService sellerService;

    @ApiOperation(value = "商铺登录")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "tel",value = "商铺手机号",required = true,dataType = "String",paramType = "query"),
//            @ApiImplicitParam(name = "password",value = "登录密码",required = true,dataType = "String",paramType = "query")
//    })
    @RequestMapping("getLogin")
    @ResponseBody
    public Map<String, Object> getLogin(String tel, String password, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> map = new HashMap<>();
        try {
            map = sellerService.BmMemberLogin(tel, password, request, response);
            return map;
        } catch (Exception e) {
            log.error("商铺登录异常："+e);
            map.put("resultMessage","商铺登录失败!");
            return map;
        }
    }

    @ApiOperation("商铺注册")
    @ResponseBody
    @RequestMapping("register")
    public Map<String,String> register(BmMember bmMember){
        Map<String, String> map = new HashMap<>();
        try {
            map = sellerService.register(bmMember);
            return map;
        } catch (Exception e) {
            log.error("商铺注册异常："+e);
            map.put("resultCode",String.valueOf(0));
            map.put("resultMessage","商铺注册失败！");
            return map;
        }
    }

    @ApiOperation("修改密码")
    @ResponseBody
    @RequestMapping("updatePassword")
    public Map<String,String> updatePassword(String oldPassword,String newPassword, String token, int id ){
        Map<String,String> map = new HashMap<>();
        try {
            map = sellerService.updatePassword(oldPassword,newPassword, token, id);
            return map;
        } catch (Exception e) {
            log.error("修改密码异常："+e);
            map.put("resultCode",String.valueOf(0));
            map.put("resultMessage","修改密码失败！");
            return map;
        }
    }

    @ApiOperation("退出登录")
    @ResponseBody
    @RequestMapping("loginOut")
    public Map<String,String> loginOut(String token){
        Map<String,String> map = new HashMap<>();
        try {
            sellerService.loginOut(token);
            map.put("status","200");
            return map;
        } catch (Exception e) {
            log.error("退出登录异常："+e);
            map.put("status","500");
            return map;
        }
    }
}
